<?php
include_once("king_head.php");
?>

<?php
if(isset($_POST["btn_upload"]))
{
	if(!is_dir("downloads_".$_SESSION['SITE_ID']))
	mkdir("downloads_".$_SESSION['SITE_ID'], 0777); 
	
	$dsq="select * from reseller where site_id=".$_SESSION['SITE_ID'];
	//echo $dsq;
	$drs=mysql_query($dsq);	
	$drw=mysql_fetch_array($drs);
	$m_size=$drw['m_size'];
	$f_size=$drw['f_size'];
	//file upload function.	
	//chmod("downloads_".$_SESSION['SITE_ID'], 0777);
	$uploaddir="downloads_".$_SESSION['SITE_ID'];
	
	//Check for Folder Size.
	$Mem=Dir_size($uploaddir);
	//echo $uploaddir.'*'.$m_size.'*'.$Mem.'*'.$f_size.'*'.$_FILES['ufile']['size'];
	if($Mem>=$m_size && $_SESSION['SITE_ID']!=101)
	{
	echo "<H4><FONT COLOR=red>No more space for this Reseller. Upload aborted.</FONT></H4>";
	}	
	//check for File Size.
	if($_FILES['ufile']['size'] > $f_size && $_SESSION['SITE_ID']!=101)
	{
	echo "<H4><FONT COLOR=red>Invalid File Size. Upload aborted.</FONT></H4>";
	}
		
	$newfile=$_FILES['ufile']['name'];
	//$fnam=substr($newfile, 0, strrpos($newfile, "."));
	//echo $fnam.'<BR>';
	//$ext=substr($newfile, strrpos($newfile,".")+1, (strlen($newfile)-strrpos($newfile,".")));
	//echo $ext;
	//$dt=date("dS M Y");
	//$newfile=$fnam.'('.$dt.').'.$ext;
	//echo $newfile;
	//print($newfile."<BR>");
		if(($Mem<$m_size) && ($_FILES['ufile']['size']<=$f_size) && $_SESSION['SITE_ID']!=101)
		{
			if(is_uploaded_file($_FILES['ufile']['tmp_name']))
			{
				move_uploaded_file($_FILES['ufile']['tmp_name'],$uploaddir."/".$newfile);
			}
			echo "<H4><FONT COLOR=red>File Uploaded Successfully!</FONT></H4>";
		}
		else
		{
			if(is_uploaded_file($_FILES['ufile']['tmp_name']))
			{
				move_uploaded_file($_FILES['ufile']['tmp_name'],$uploaddir."/".$newfile);
			}
			echo "<H4><FONT COLOR=red>File Uploaded Successfully!</FONT></H4>";
		}
}
?>
<?php
 function Dir_size($dir_name)
 {
$filesizex=0;
$hnd=opendir("./".$dir_name);
while($filex=readdir($hnd))
{	
	if($filex!="." && $filex!="..")
	{//echo "File: ".$filex." Size: ". (filesize("./tmp_upload/".$filex)/1024)."\n";
	$filesizex = $filesizex + filesize("./".$dir_name."/".$filex);
	clearstatcache();
	 }	
}
closedir($hnd);
//echo "Directory size: ". ($filesizex/1024);
return $filesizex;
 }
?>
<CENTER>
<H2> File Upload </H2><BR><BR>
<FORM METHOD=POST enctype="multipart/form-data" ACTION="file_upload.php?site_id=<?php echo $_SESSION['SITE_ID'];?>">
<table width="52%" border="0" cellpadding="0" cellspacing="0" bordercolor="#111111" style="border-collapse: collapse">
  <tr>
    <td width="39%">File to Upload:</td>
    <td width="61%"><input type="file" name="ufile" size="20"/></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;<input type="submit" value="Upload" name="btn_upload"/></td>
  </tr>
</table>
</FORM>
</CENTER>
<?php
include_once("king_foot.php");
?>